const SortingAlgorithms = require("../template")

function testSortingPerformance() {
  const sorter = new SortingAlgorithms()

  console.log(
    "1. 小数组性能测试:",
    (() => {
      const smallArray = Array.from({ length: 10 }, () => Math.floor(Math.random() * 100))
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => {
        const start = performance.now()
        sorter[algo]([...smallArray])
        const end = performance.now()
        return (end - start) < 100 // 小数组应该在100ms内完成
      })
    })()
  )

  console.log(
    "2. 中等数组性能测试:",
    (() => {
      const mediumArray = Array.from({ length: 100 }, () => Math.floor(Math.random() * 1000))
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => {
        const start = performance.now()
        sorter[algo]([...mediumArray])
        const end = performance.now()
        return (end - start) < 1000 // 中等数组应该在1秒内完成
      })
    })()
  )

  console.log(
    "3. 大数组高效算法性能测试:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, () => Math.floor(Math.random() * 10000))
      const efficientAlgorithms = ["mergeSort", "quickSort", "heapSort"]
      return efficientAlgorithms.every(algo => {
        const start = performance.now()
        sorter[algo]([...largeArray])
        const end = performance.now()
        return (end - start) < 2000 // 高效算法应该在2秒内完成
      })
    })()
  )

  console.log(
    "4. 最坏情况性能测试:",
    (() => {
      const worstCaseArray = Array.from({ length: 100 }, (_, i) => 100 - i)
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => {
        const start = performance.now()
        sorter[algo]([...worstCaseArray])
        const end = performance.now()
        return (end - start) < 1000 // 最坏情况应该在1秒内完成
      })
    })()
  )

  console.log(
    "5. 最好情况性能测试:",
    (() => {
      const bestCaseArray = Array.from({ length: 100 }, (_, i) => i + 1)
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => {
        const start = performance.now()
        sorter[algo]([...bestCaseArray])
        const end = performance.now()
        return (end - start) < 1000 // 最好情况应该在1秒内完成
      })
    })()
  )

  console.log(
    "6. 性能一致性测试:",
    (() => {
      const testArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      const results = algorithms.map(algo => sorter[algo]([...testArray]))
      return results.every(result => JSON.stringify(result) === JSON.stringify([1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]))
    })()
  )

  console.log(
    "7. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(arr) {
          const sorter = new SortingAlgorithms()
          const start = performance.now()
          const result = sorter.bubbleSort([...arr])
          const end = performance.now()
          return result.map(item => item * this.multiplier)
        }
      }
      const result = perfObj.processPerformance([3, 1, 2])
      return JSON.stringify(result) === JSON.stringify([2, 4, 6])
    })()
  )

  console.log(
    "8. 复杂对象this绑定性能测试:",
    (() => {
      const perfConfigObj = {
        config: { offset: 5 },
        processWithConfig: function(arr) {
          const sorter = new SortingAlgorithms()
          const start = performance.now()
          const result = sorter.selectionSort([...arr])
          const end = performance.now()
          return result.map(item => item + this.config.offset)
        }
      }
      const result = perfConfigObj.processWithConfig([2, 1, 3])
      return JSON.stringify(result) === JSON.stringify([6, 7, 8])
    })()
  )
}

testSortingPerformance()
